//
//  PictureController.m
//  11.17
//
//  Created by M on 16/11/17.
//  Copyright © 2016年 apple1512. All rights reserved.
//

#import "PictureController.h"

@interface PictureController () <NSURLSessionDataDelegate>{

    NSMutableData *mutableData;
    
}

@property (weak, nonatomic) IBOutlet UIProgressView *onProgress;

@property (weak, nonatomic) IBOutlet UIImageView *onImage;

@end

@implementation PictureController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (IBAction)onStart:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fi1.s2.dpfile.com%2Fwed%2F_myM9xAlmP31b4G0Sj3gjQNPF5m_Gatpo_NXxwKhcIVSRDgQBKrxzU_4qCzw6vujEKQ_oZ_owvl3Ag3Ctjd26Q.jpg&thumburl=http%3A%2F%2Fimg2.imgtn.bdimg.com%2Fit%2Fu%3D1215899776%2C3157702292%26fm%3D21%26gp%3D0.jpg"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionDataTask *task = [session dataTaskWithURL:url];
    [task resume];
    self.onProgress.progress = 0.0;
    self.onImage.image = nil;
    //NSLog(@"start!!!!");
}



-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
    
    self.onImage.image = [UIImage imageWithData:mutableData];
    
    
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
   self.onProgress.progress = (float)dataTask.countOfBytesReceived/(float)dataTask.countOfBytesExpectedToReceive;
    NSLog(@"%f",self.onProgress.progress);
    if(!mutableData){
        mutableData = [NSMutableData new];
    }
    [mutableData appendData:data];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
